<?php

namespace app\home\controller;

use \think\Db;
use \app\common\Pay;
use \app\common\TenWePay;
use \app\common\model\Config;

class Test
{

	public function __construct()
	{

	}

	public function index()
	{

	}

	public function trade_check()
	{
		if(!is_dir(RUNTIME_PATH . 'logs')) {
			$list = _dir(_DATA_ . 'logs', true);
		} else {
			$list = _dir(RUNTIME_PATH . 'logs', true);
		}
		//print_r($list);
		$trades = [];
		foreach($list as $value) {
			$content = file_get_contents($value['name']);
			preg_match_all('/\[([^\]]+)\]\s\"POST\s\/pay\/cash\/query\"(.*)\r\n(.*)\r\n(.*)ERROR_HTTP_CODE_0\"/', $content, $matches);
			foreach($matches[3] as $trade) {
				$body = preg_replace('/\[([^\]]+)\]\s/', '', $trade);
				$body = json_decode($body, true);
				if($body) {
					$trades[] = $body;
				}
			}
		}
		//ksort($trades);
		$sortes = [];
		foreach($trades as $key => $value) {
			$sortes[$key] = $value['out_trade_no'];
		}
		array_multisort($sortes, SORT_ASC, $trades);
		$trades = array_values($trades);
		//print_r($trades);
		foreach($trades as $key => $value) {
			$trade = Db::name('trade')->where('out_trade_no', '=', $value['out_trade_no'])->find();
			if($trade) {
				//print_r($trade);
			} else {
				echo 'Trade ' . $value['out_trade_no'] . ' not found.' . "\r\n";
				/*
				$merchant = \app\common\Pay::merchant($merchant, ['person_id' => $value['person_id']]);
				$res = \app\pay\controller\Index::query($merchant, $value['out_trade_no']);
				print_r($res);
				*/
			}
		}
	}

}

